package br.com.condominio.controller;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import br.com.condominio.entity.Residencial;
import br.com.condominio.service.ResidencialService;

@ViewScoped
@ManagedBean(name = "residencialBean")
public class ResidencialBean extends BaseBean{
	
	private static final long serialVersionUID = 1L;
	@ManagedProperty("#{residencialService}")
	private ResidencialService model;
	private Residencial residencial;
	private List<Residencial> residencials;
	private String nomeResidencial;
	private boolean editMode;
	
	public ResidencialBean() {
		residencial = new Residencial();
	}

	public ResidencialService getModel() {
		return model;
	}

	public void setModel(ResidencialService model) {
		this.model = model;
	}

	public Residencial getResidencial() {
		return residencial;
	}

	public void setResidencial(Residencial residencial) {
		this.residencial = residencial;
	}
	
	public void salvar(){
		System.out.println("idf residencial " +residencial.getResCodigo());
		if(residencial.getResCodigo() == null || residencial.getResCodigo().intValue() == 0){
			residencial = model.createResidencial(residencial);
			residencial = new Residencial();
			addInfoMessage("Residencial criado com sucesso.");
		}else{
			model.updateResidencial(residencial);
			addInfoMessage("Residencial alterado com sucesso.");
		}
	}
	
	public void delete(){
		model.deleteResidencial(residencial);
		if(nomeResidencial != null && !nomeResidencial.isEmpty())
			residencials = model.getResidencialsByName(nomeResidencial);
		else
			residencials = model.getAllResidencials();
		
		//return "";
	}

	public void create(){
		this.residencial = new Residencial();
		this.editMode = true;
	}
	
	public void update(){
		this.editMode = true;
	}
	
	public void cancel(){
		this.editMode = false;
	}

	public void filtrarResidencial(AjaxBehaviorEvent event){
		if(nomeResidencial != null && !nomeResidencial.isEmpty()){
			residencials = model.getResidencialsByName(nomeResidencial);
		}else{
			residencials = model.getAllResidencials();
		}
	}

	public List<Residencial> getResidencials() {
		if(residencials == null){
			residencials = model.getAllResidencials();
		}
		return residencials;
	}

	public void setResidencials(List<Residencial> residencials) {
		this.residencials = residencials;
	}

	public String getNomeResidencial() {
		return nomeResidencial;
	}

	public void setNomeResidencial(String nomeResidencial) {
		this.nomeResidencial = nomeResidencial;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}
}
